/* eslint-disable no-undef */
/*
 * @Description:
 * @Author: cgt
 * @Date: 2021-10-20 17:40:41
 * @LastEditors: askmewhat
 * @LastEditTime: 2022-05-11 08:51:07
 */
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const { name } = require('./package.json');
const { default: MergeOutputPlugin } = require('./plugins/MergeOutputPlugin');

function checkLifecycleEvent(event) {
  return process.env.npm_lifecycle_event === event;
}

function resolve(dir) {
  return path.join(__dirname, dir);
}

/**
 * 打包api
 */
var apiConfig = {
  configureWebpack: {
    resolve: {
    },
    entry: './src/fzcp_api/main.js',
    output: {
      // 把子应用打包成 umd 库格式
      filename: `${name}.js`,
      library: `${name}`,
      libraryTarget: 'umd',
      libraryExport: 'default',
      jsonpFunction: `Jsonp_${name}`
    }
  }
};

/**
 * 示例配置
 */
var exampleConfig = {
  outputDir: './lib',
  publicPath: './',
  indexPath: 'index.html',
  assetsDir: 'static',
  lintOnSave: true,
  productionSourceMap: false,
  devServer: {
    port: 8081,
    proxy: {
      '/fzcpserver/': {
        target: 'http://192.168.0.142:9000',
        ws: false,
        changeOrigin: true
      },
      '/figure/': {
        target: 'http://192.168.30.13:30001',
        ws: false,
        changeOrigin: true
      }
    }
  },
  configureWebpack: {
    entry: './src/example.js',
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    optimization: { },
    output: {
      filename: 'FrameworkLib.js',
      library: 'FrameworkLib',
      libraryTarget: 'umd'
    }
  }
};

/**
 * 打包API配置
 * @returns
 */
var buildConfig = {
  outputDir: './lib',
  publicPath: './',
  indexPath: 'index.html',
  assetsDir: 'static',
  lintOnSave: false,
  productionSourceMap: false,
  configureWebpack: {
    entry: './src/main.js',
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    optimization: {
    },
    output: {
      filename: 'FrameworkLib.js',
      library: 'FrameworkLib',
      libraryTarget: 'umd'
    },
    plugins:[
        new MergeOutputPlugin(),
    ]
  },
};

/**
 * 获取webpack 配置
 * @returns
 */
var getConfig = () => {
  if(checkLifecycleEvent('generateapi')) {
    console.log('generateapi');
    return apiConfig;
  }else if(checkLifecycleEvent('example')) {
    console.log('example');
    return exampleConfig;
  }
  console.log('build');
  return buildConfig;


};
module.exports = getConfig();
